package com.google.code.mergeinformer.model;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import javax.validation.constraints.Size;

/**
 * @author Dmitry Golubev
 */
@Entity
@Table(name = "branch")
public class Branch extends DefaultEntity implements Comparable<Branch> {

    @NotBlank
    @Size(max = 255)
    @Column(name = "name", nullable = false)
    private String name;

    @NotBlank
    @Size(max = 500)
    @Column(name = "url", nullable = false)
    private String url;

    @ManyToOne
    @JoinColumn(name = "id_project")
    private Project project;

    protected Branch() {
    }

    public Branch(String name, String url) {
        this.name = name;
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public String getName() {
        return name;
    }

    @JsonIgnore
    public Project getProject() {
        return project;
    }

    void setProject(Project project) {
        this.project = project;
    }

    @Override
    public int compareTo(Branch that) {
        if (!name.equals(that.name)) {
            if ("trunk".equals(name)) {
                return -1;
            }
            if ("trunk".equals(that.name)) {
                return 1;
            }
        }
        return name.compareTo(that.name);
    }
}
